package com.mproject.activity;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

import com.mproject.customclass.Category;
import com.mproject.network.IVLE_API_Helper;
import com.mproject.network.ProgressBarManager;

public class AddProjectActivity extends Activity implements OnClickListener{

	private TextView user_name;
	private Button confirm;

	private TextView project_title;
	private TextView project_description;
	private Spinner project_category;

	public static SharedPreferences mypref;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_project_wizard); 
		mypref=getSharedPreferences("My_Prefs",Activity.MODE_WORLD_WRITEABLE);

		project_title = (TextView)findViewById(R.id.add_project_set_title);
		project_description = (TextView)findViewById(R.id.add_project_set_description);
		project_category = (Spinner)findViewById(R.id.add_project_set_category);

		user_name = (TextView)findViewById(R.id.add_project_user_name);
		confirm = (Button)findViewById(R.id.add_project_confirm);
		confirm.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == confirm) {
			if (project_title.getText().toString().equals("")) {
				AlertDialog.Builder builder = new AlertDialog.Builder(AddProjectActivity.this);
				builder.setMessage("Title cannot be empty!")
				.setNegativeButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();
					}

				});
				AlertDialog alert = builder.create();
				alert.show();
			}
			else {
				ProgressBarManager manager=new ProgressBarManager(AddProjectActivity.this)
				{

					protected void onPreExecute() {

						Log.v("manager"," in pre execute");

						dialog.setMessage("Adding project please wait...");
						dialog.setIndeterminate(true);

						// dialog.setTitle("Manager");
						dialog.show();
					}

					protected Boolean doInBackground(String... params) 
					{

						try {
							//get category
							List<Category> category_list = IVLE_API_Helper.getCategory(mypref.getString("IVLE_Token", ""));
							String category_id = "";
							for (Category category : category_list) {
								if (project_category.getSelectedItem().toString().equals(category.getCategory_name())) {
									category_id = category.getCategory_id();
									Log.e("Category",category.getCategory_name());
									Log.e("Category",category.getCategory_id());
									break;
								}
							}
							//create project
							IVLE_API_Helper.createProjet(mypref.getString("IVLE_Token", ""), project_title.getText().toString(), project_description.getText().toString(), category_id);
							return true;
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
							return false;
						}


					}
					protected void onPostExecute(Boolean result) {
						this.getDialog().dismiss();
						if (result == false) {
							AlertDialog.Builder builder = new AlertDialog.Builder(AddProjectActivity.this);
							builder.setMessage("No internet connection")
							.setNegativeButton("OK", new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog, int id) {
									dialog.cancel();
								}

							});
							AlertDialog alert = builder.create();
							alert.show();
						}
						else {

							finish();

						}
					}
				};
				manager.execute();
			}



		}

	}
}
